/**
 * 
 * Sun CMS
 * 
 * @author			Burning
 * @copyright		Copyright (c) 2009, IsunCms.Com.
 * @license			http://www.isuncms.com/license.htm
 * @link				http://www.isuncms.com/
 * @since				Version 1.0 Beta
 * 
 */

var article = {
	moveArIds: '',
	viewThumb: function(str)
	{
		if( str == 'show' )
		{
			var thumb_pic = $("#thumb_pic").val();
			if( thumb_pic == '' ) return false;
			if( thumb_pic.match(/\.(jpg|jpeg|gif|bmp|png)$/i) ) $("#view_thumb").attr('src','uploads/thumb/'+thumb_pic).show();
			else $("#thumb_pic").val('');
		}
		else $("#view_thumb").hide();
	},
	getCity: function(n)
	{
		var n = $("#province").val();
		if( n == 0 ) $("#city").html("<option value='0'></option>");
		else
		{
			$.ajax({
				type: "POST",
				url: SITE_URL+SCRIPT+"?m=article&a=getcity",
				data: {'pid':n},
				dataType:'json',
				success: function(rs)
				{
					if(rs)
					{
						$("#city").html('');
						var no = rs.length;
						if( no == 0 ) $("#city").append("<option value='0'></option>");
						else 
						{
							for(i=0; i<no; i++){$("#city").append("<option value="+rs[i].id+">&nbsp;&nbsp;&nbsp;"+rs[i].name+"</option>");}
						}
					}
					else return false;
				}
			});
		}
	},
	checkboxSelect: function(n){($("#tr_"+n+" .checkbox").attr("checked")==true)?$("#tr_"+n).addClass('tr_selected'):$("#tr_"+n).removeClass("tr_selected");},
	preview: function(id)
	{
		var ids = getChecked();
		if(ids=='')salert('请选择要预览的文档！');
		else if(ids.length>1)salert('参数错误，请重新操作！');
		else{parent.dialog.iframe(1,'文档预览','?m=dialog&a=preview&id='+ids[0],2,0,1);}
	},
	newArticle: function(){parent.dialog.iframe(1,'文档栏目选择','?m=article&a=new&do=newarticle',0,1,2);},
	newMore: function()
	{
		var ids = getChecked();
		if(ids=='')salert('请选择要续发的文档！');
		else if(ids.length>1)salert('参数错误，请重新操作！');
		else{window.location=SITE_URL+SCRIPT+'?m=article&a=new&do=newmore&id='+ids[0];}
	},
	editSelect: function(id)
	{
		var ids = (typeof id == "undefined")?getChecked():[id];
		if(ids=='')salert('请选择要编辑的文档！');
		else if(ids.length>1)salert('参数错误，请重新操作！');
		else
		{
			$.ajax({
				type: "POST",
				url: SITE_URL+SCRIPT+"?m=article&a=edit&do=editcheck&id="+ids[0],
				dataType:'json',
				error: function (xmlHttpRequest, error) {
					salert('从服务器获取数据异常！');
				},
				success: function(rs)
				{
					if(rs)
					{
						parent.dialog.iframe(1,'编辑模式选择','?m=article&a=edit&do=editarticle&id='+ids[0],0,1,2);
					}
					else{window.location=SITE_URL+SCRIPT+'?m=article&a=edit&do=editarticle&id='+ids[0]+'&page=1';}
				}
			});
		}
	},
	copy: function()
	{
		var ids = getChecked();
		if(ids=='')salert('请选择要复制的文档！');
		else{var strIds = ids.join('_');parent.dialog.iframe(1,'请选择要复制到的分类','?m=article&a=mag&do=newcopy&ids='+strIds,1,1,2);}
	},
	move: function()
	{
		var ids = getChecked();
		if(ids=='')salert('请选择要转移的文档！');
		else{var strIds = ids.join('_');parent.dialog.iframe(1,'请选择要转移到的分类','?m=article&a=mag&do=newmove&ids='+strIds,1,1,2);}
	},
	articleDelConfirm: function()
	{
		var ids = getChecked();
		if(ids=='')salert('请选择要删除的文档！');
		else{var strIds = ids.join('_');parent.dialog.query('此操作不可恢复，是否确定删除？',"app.articleDel('"+strIds+"')");}
	},
	thumbUpload: function(){parent.dialog.iframe(1,'缩略图上传','?m=dialog&a=thumb',1,1,3);},
	getSpecial: function(){parent.dialog.iframe(1,'专题选择','?m=dialog&a=special',1,1,2);}
}

var autoSave = {
	msg: [' 秒后自动保存', '正在保存...', ' 已自动保存', '自动保存失败', '禁止自动保存', '于 ', '内容为空，跳过保存', 'AJAX数据获取发生异常', '内容无变化，跳过保存'],
	saveData: '',
	autoSaveTime: 120,
	autoSaveMode: 1,
	saveTime: 120,
	run: function ()
	{
		if(this.autoSaveMode==1)
		{
			this.saveTime = this.saveTime-1;
			if(this.saveTime >= 1)
			{
				$("#autosave #savemsg").text(this.saveTime+' '+this.msg[0]);
				window.setTimeout("autoSave.run()", 1000);
			}
			else{$("#autosave #savemsg").text(this.msg[1]);this.save();}
		}
	},
	start: function (){if(this.autoSaveMode==1)return false;else{this.autoSaveMode=1;this.run();}},
	suspend: function (){this.autoSaveMode=2;this.run();},
	disable: function (){$("#autosave #savemsg").text(this.msg[4]);this.autoSaveMode=2;this.saveTime=this.autoSaveTime;this.run();},
	save: function ()
	{
		this.autoSaveMode=2;
		this.saveTime=this.autoSaveTime;
		var datas = KE.util.getData('content');
		if(datas==''||datas=='<br />'||datas=='<p>&nbsp;</p>') {this.error(1);}
		else if(datas==this.saveData){this.error(4);}
		else
		{
			this.saveData = datas;
			$.ajax({
				type: "POST",
				url: SITE_URL+"editor.php?m=data&a=savedata",
				data: {'datas':datas},
				dataType:'json',
				error: function (xmlHttpRequest, error) {
					autoSave.error(3);
				},
				success: function(rs)
				{
					if(rs.ok==1)
					{
						$("#autosave #savemsg").text(autoSave.msg[5]+rs.time+autoSave.msg[2]);
						autoSave.autoSaveMode=1;
						window.setTimeout("autoSave.run()", 3000);
					}
					else{this.error(2);}
				}
			});
		}
	},
	error: function(n)
	{
		var text;
		if(n==1) text = this.msg[6];
		else if(n==2) text = this.msg[3];
		else if(n==3) text = this.msg[7];
		else if(n==4) text = this.msg[8];
		$("#autosave #savemsg").text(text);
		this.autoSaveMode=1;
		window.setTimeout("autoSave.run()", 3000);
	},
	recover: function()
	{
		$.ajax({
			type: "POST",
			url: SITE_URL+"editor.php?m=data&a=getlist",
			dataType:'json',
			error: function (xmlHttpRequest, error) {
				$("#autosave #s_recover").show();
			},
			success: function(rs)
			{
				$("#autosave .recover").html('');
				var no = rs.length;
				if( no != 0 ){for(i=0; i<no; i++){$("#autosave .recover").append("<li><a href='javascript:void(0)' onclick='autoSave.insertData("+rs[i].id+")'>"+rs[i].time+"</a></li>");}}
				$("#autosave #s_recover").show();
			}
		});
	},
	insertData:function(n)
	{
		$.ajax({
			type: "POST",
			url: SITE_URL+"editor.php?m=data&a=getdata",
			data: {'id':n},
			dataType:'json',
			error: function (xmlHttpRequest, error) {
				$("#autosave #s_recover").hide();
			},
			success: function(rs)
			{
				autoSave.clearEditor('content');autoSave.insertHtml('content', rs.data);
				$("#autosave #s_recover").hide();
			}
		});
	},
	clearEditor: function (id) {
		KE.g[id].iframeDoc.open();
		KE.g[id].iframeDoc.write(KE.util.getFullHtml(id));
		KE.g[id].iframeDoc.close();
		KE.g[id].newTextarea.value = '';
	},
	insertHtml: function (id, html) {
		KE.util.focus(id);
		KE.util.selection(id);
		KE.util.insertHtml(id, html);
	}
}

var page = {
	go: function(item)
	{
		var url = $("#curr_url").val(), pageNo = $("#page_no").val(), currPage = $("#curr_page").val();
		var gourl = '';
		switch(item)
		{
			case 'first':
				if(currPage==1){salert("已经是第一页！");return false;}
				gourl = url+'&page=1';
				break;
			case 'prev':
				if(currPage-1>=1){gourl = url+'&page='+(currPage-1);}else{return false};
				break;
			case 'next':
				if(currPage+1<=pageNo){gourl = url+'&page='+(++currPage);}else{return false};
				break;
			case 'last':
				if(currPage==pageNo){salert("已经是最后一页！");return false;}
				gourl = url+'&page='+pageNo;
				break;
		}
		window.location=gourl;
	},
	gopage: function()
	{
		var url = $("#curr_url").val(), pageNo = $("#page_no").val(), currPage = $("#curr_page").val();
		var pageGo = $("#gopage").val();
		if(pageGo>=1&&pageGo<=pageNo)window.location=url+'&page='+pageGo;
		else{salert('输入的页码不正确！');return false;}
	}
}